﻿using System;
using System.Collections.Generic;
using RuntimeGizmos;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

namespace Sizebox.CharacterEditor
{
	// Token: 0x02000283 RID: 643
	public class WeldToolView : BaseAdvancedView
	{
		// Token: 0x170001A4 RID: 420
		// (get) Token: 0x06000E1D RID: 3613 RVA: 0x0006E033 File Offset: 0x0006C233
		// (set) Token: 0x06000E1E RID: 3614 RVA: 0x0006E03C File Offset: 0x0006C23C
		private WeldToolViewState State
		{
			get
			{
				return this._state;
			}
			set
			{
				bool flag = value == WeldToolViewState.SelectingBones;
				this.selectionPanel.SetActive(flag);
				this.weldPanel.SetActive(!flag);
				if (flag)
				{
					base.ShowHandles(null);
				}
				else
				{
					this.handleManager.DisableHandlesMode();
					this.Controller.commandEnabled = false;
				}
				this._state = value;
			}
		}

		// Token: 0x06000E1F RID: 3615 RVA: 0x0006E09C File Offset: 0x0006C29C
		protected override void Awake()
		{
			base.Awake();
			this.gizmo = Camera.main.GetComponent<TransformGizmo>();
			this.confirmBonesButton.onClick.AddListener(new UnityAction(this.OnConfirmBones));
			this.backButton.onClick.AddListener(new UnityAction(this.OnBack));
			this.weldButton.onClick.AddListener(new UnityAction(this.OnWeld));
		}

		// Token: 0x06000E20 RID: 3616 RVA: 0x0006E114 File Offset: 0x0006C314
		protected override void OnEnable()
		{
			if (!this.Controller.selectedEntity || !this.Controller.selectedEntity.GetComponent<WeldTool>())
			{
				base.gameObject.SetActive(false);
				return;
			}
			base.OnEnable();
			this.selectedBones.Clear();
			this.State = WeldToolViewState.SelectingBones;
		}

		// Token: 0x06000E21 RID: 3617 RVA: 0x0006DF85 File Offset: 0x0006C185
		protected override void OnDisable()
		{
			base.OnDisable();
			this.Controller.commandEnabled = true;
		}

		// Token: 0x06000E22 RID: 3618 RVA: 0x0006E178 File Offset: 0x0006C378
		private void Update()
		{
			if (this.State == WeldToolViewState.SelectingBones)
			{
				this.confirmBonesButton.interactable = (this.handleManager.TargetHandles.Count > 0);
			}
			else
			{
				bool mouseButtonDown = Input.GetMouseButtonDown(0);
				if (mouseButtonDown)
				{
					Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
					RaycastHit[] array = Physics.RaycastAll(ray, float.PositiveInfinity);
					if (array.Length > 0)
					{
						foreach (RaycastHit raycastHit in array)
						{
							EntityBase componentInParent = raycastHit.collider.transform.GetComponentInParent<EntityBase>();
							if (this.SelectObject(componentInParent))
							{
								break;
							}
						}
					}
				}
				this.weldButton.interactable = this.selectedObject;
			}
		}

		// Token: 0x06000E23 RID: 3619 RVA: 0x0006E24C File Offset: 0x0006C44C
		private bool SelectObject(EntityBase obj)
		{
			EntityBase component = this.handleManager.TargetEditor.GetComponent<EntityBase>();
			if (!obj || obj == component || obj.GetComponent<CityBuilder>() || obj.isPlayer)
			{
				return false;
			}
			this.selectedObject = obj;
			this.gizmo.SetTarget(this.selectedObject.transform);
			this.selectObjectText.text = this.selectedObject.name;
			return true;
		}

		// Token: 0x06000E24 RID: 3620 RVA: 0x0006E2D4 File Offset: 0x0006C4D4
		private void OnWeld()
		{
			if (this.selectedBones.Count <= 0)
			{
				return;
			}
			if (!this.selectedObject)
			{
				return;
			}
			WeldTool component = this.Controller.selectedEntity.GetComponent<WeldTool>();
			List<string> list = component.Weld(this.selectedObject, this.selectedBones);
			this.extraView.UpdateKeys();
			foreach (string item in list)
			{
				if (!this.extraView.Keys.Contains(item))
				{
					this.extraView.Keys.Add(item);
				}
			}
			this.extraView.SaveKeys(this.Controller.selectedEntity.GetComponent<CharacterEditor>());
			this.OnBack();
		}

		// Token: 0x06000E25 RID: 3621 RVA: 0x0006E3C0 File Offset: 0x0006C5C0
		private void OnConfirmBones()
		{
			this.selectedBones.Clear();
			foreach (SkeletonEditHandle skeletonEditHandle in this.handleManager.TargetHandles)
			{
				this.selectedBones.Add(skeletonEditHandle.EditBone);
			}
			this.State = WeldToolViewState.SelectingObject;
		}

		// Token: 0x06000E26 RID: 3622 RVA: 0x0006E440 File Offset: 0x0006C640
		private void OnBack()
		{
			this.State = WeldToolViewState.SelectingBones;
			this.selectedBones.Clear();
			this.selectedObject = null;
			this.selectObjectText.text = string.Empty;
			this.gizmo.SetTarget(null);
		}

		// Token: 0x0400130A RID: 4874
		[Header("Weld Tool Required References")]
		[Space]
		[SerializeField]
		private ExtraView extraView;

		// Token: 0x0400130B RID: 4875
		[SerializeField]
		private GameObject selectionPanel;

		// Token: 0x0400130C RID: 4876
		[SerializeField]
		private GameObject weldPanel;

		// Token: 0x0400130D RID: 4877
		[SerializeField]
		[Space]
		private Button confirmBonesButton;

		// Token: 0x0400130E RID: 4878
		[SerializeField]
		private Button backButton;

		// Token: 0x0400130F RID: 4879
		[SerializeField]
		private Button weldButton;

		// Token: 0x04001310 RID: 4880
		[SerializeField]
		[Space]
		private Text selectObjectText;

		// Token: 0x04001311 RID: 4881
		private EntityBase selectedObject;

		// Token: 0x04001312 RID: 4882
		private List<EditBone> selectedBones = new List<EditBone>();

		// Token: 0x04001313 RID: 4883
		private WeldToolViewState _state;

		// Token: 0x04001314 RID: 4884
		private TransformGizmo gizmo;
	}
}
